package com.nicoB.tetris {
	import com.nicoB.tetris.constants.ActionsType;	
	import com.nicoB.tetris.events.TetrisEvent;	
	
	import flash.display.Stage;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;		

	/**
	 * @author Papa et Maman
	 */
	public class KeyboardManager extends EventDispatcher {

		private var _stage : Stage;

		public function KeyboardManager(stage : Stage) {
			_stage = stage;
		}
		
		
		private function _onKeyDown(e : KeyboardEvent) : void {
			
			var ev : TetrisEvent = new TetrisEvent(TetrisEvent.CLIENT_ACTION);
			
			if (e.keyCode == Keyboard.LEFT) {
				ev.action = ActionsType.MOVE_LEFT; 
			}
			
			if (e.keyCode == Keyboard.RIGHT){
				ev.action = ActionsType.MOVE_RIGHT; 
			}
			
			if (e.keyCode == Keyboard.DOWN) {
				ev.action = ActionsType.MOVE_DOWN; 
			}
			
			if (e.keyCode == Keyboard.SPACE) {
				ev.action = ActionsType.ROTATE_BLOCK; 
			}
			
			
			dispatchEvent(ev);
		}

		
		public function start() : void {
			_stage.addEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown, false, 0, true);
		}
		
		public function stop() : void {
			_stage.removeEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);
		}
		
		
	}
}
